   package com.javalong.affairs.module.renwujinzhang.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.constant.StatusCode;
import com.javalong.affairs.base.model.SimpleMessage;
import com.javalong.affairs.base.utils.TimeUtils;

public class TaskAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<SimpleMessage> msgList;
	private Type type;
	private Context mContext;

	public TaskAdapter(Context context, List<SimpleMessage> msgList, Type type) {
		mContext = context;
		inflater = LayoutInflater.from(context);
		this.msgList = msgList;

		this.type = type;
	}

	@Override
	public int getCount() {
		return msgList.size() + 1;
	}

	@Override
	public Object getItem(int position) {
		if (position == 0) {
			return null;
		}
		return msgList.get(position - 1);
	}

	@Override
	public long getItemId(int position) {
		return position + 1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SimpleMessage msgInfo = null;
		ViewHolder holder = null;
		if (convertView != null) {
			holder = (ViewHolder) convertView.getTag();
		} else {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_notification_receive,
					null);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.tv_sender = (TextView) convertView
					.findViewById(R.id.tv_sender);
			holder.tv_status = (TextView) convertView
					.findViewById(R.id.tv_status);
			holder.tv_dept = (TextView) convertView.findViewById(R.id.tv_dept);
			convertView.setTag(holder);
		}
		if (position == 0) {
			holder.tv_time.setText("时间");
			holder.tv_title.setText("标题");
			holder.tv_status.setText("状态");
			holder.tv_sender.setText("发送人");
			holder.tv_dept.setText("部门");
		} else {
			msgInfo = (SimpleMessage) getItem(position);
			if (msgInfo.time != null) {
				holder.tv_time.setText(TimeUtils.getFormatTime(Long
						.parseLong(msgInfo.time)));
			}
			if (msgInfo.title != null) {
				holder.tv_title.setText(msgInfo.title);
			}
			if (msgInfo.receiveStatus != null) {
				int receiveStatus = Integer.parseInt(msgInfo.receiveStatus);
				if (receiveStatus == 0) {
					holder.tv_status.setTextColor(Color.RED);
				} else {
					holder.tv_status.setTextColor(Color.BLACK);
				}
				holder.tv_status.setText(StatusCode
						.getStatisticStatus(receiveStatus));
			}
			holder.tv_dept.setText(msgInfo.sendSection);
			holder.tv_sender.setText(msgInfo.sendName);
		}
		return convertView;
	}

	class ViewHolder {
		public TextView tv_time;
		public TextView tv_title;
		public Button bt_update;
		public Button bt_delete;
		public Button bt_search;
		public TextView tv_status;
		public TextView tv_sender;
		public TextView tv_dept;
	}

	public enum Type {
		Receive, Send
	}

}
